package com.nanjia.boot.brand.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nanjia.boot.brand.model.form.BrandInfoForm;
import com.nanjia.boot.brand.model.query.BrandInfoQuery;
import com.nanjia.boot.brand.model.vo.BrandInfoVO;
import com.nanjia.boot.brand.service.BrandInfoService;
import com.nanjia.boot.common.result.PageResult;
import com.nanjia.boot.common.result.Result;
import com.nanjia.boot.common.util.IPUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 品牌信息前端控制层
 *
 * @author 周洋
 * @since 2025-07-04 15:18
 */
@Tag(name = "品牌信息接口")
@RestController
@RequestMapping("/api/v1/brand-info")
@RequiredArgsConstructor
public class BrandInfoController  {

    private final BrandInfoService brandInfoService;

    @Operation(summary = "品牌信息分页列表")
    @GetMapping("/page")
    public PageResult<BrandInfoVO> getBrandInfoPage(BrandInfoQuery queryParams ) {
        IPage<BrandInfoVO> result = brandInfoService.getBrandInfoPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增品牌信息")
    @PostMapping
    public Result<Void> saveBrandInfo(@RequestBody @Valid BrandInfoForm formData , HttpServletRequest request) {
        boolean result = brandInfoService.saveBrandInfo(formData, IPUtils.getIpAddr(request));
        return Result.judge(result);
    }

    @Operation(summary = "获取品牌信息表单数据")
    @GetMapping("/{id}/form")
    public Result<BrandInfoForm> getBrandInfoForm(
        @Parameter(description = "品牌信息ID") @PathVariable Long id
    ) {
        BrandInfoForm formData = brandInfoService.getBrandInfoFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改品牌信息")
    @PutMapping(value = "/{id}")
    public Result<Void> updateBrandInfo(
            @Parameter(description = "品牌信息ID") @PathVariable Long id,
            @RequestBody @Validated BrandInfoForm formData,HttpServletRequest request
    ) {
        boolean result = brandInfoService.updateBrandInfo(id, formData,IPUtils.getIpAddr(request));
        return Result.judge(result);
    }

    @Operation(summary = "删除品牌信息")
    @DeleteMapping("/{ids}")
    public Result<Void> deleteBrandInfos(
        @Parameter(description = "品牌信息ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = brandInfoService.deleteBrandInfos(ids);
        return Result.judge(result);
    }
}
